package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrderDto;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.entity.OrderDetail;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrdersService;
import com.itheima.reggie.service.ShoppingCartService;
import com.itheima.reggie.utils.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/order")
public class OrdersController {
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 订单提交(移动端)
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> postSubmit(@RequestBody Orders orders){
        log.info("提交订单:{}",orders);

        // 调service方法
        ordersService.postOrders(orders);

        return R.success("订单提交成功!");
    }

    /**
     * 订单查询(移动端)
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R<Page<OrderDto>> userPage(Long page, Long pageSize){
        log.info("订单查询页码:{},订单查询条数:{}",page,pageSize);
        //分页查询
        Page<OrderDto> page1 = ordersService.showAllOrder(page, pageSize);

        return R.success(page1);
    }

    /**
     * 网页端订单查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page<Orders>> showOrders(Long page, Long pageSize, Long number,String beginTime,String endTime){
        log.info("分页页码:{},分页条数:{},订单id:{},开始时间:{},截止时间:{}",page,pageSize,number,beginTime,endTime);

        // 分页构造器
        Page<Orders> ordersPage = new Page<>(page,pageSize);
        // 条件查询
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(number != null,Orders::getNumber,number);   // 订单id查询
        if (beginTime != null && endTime != null){  // 判断起始时间与结束时间不为空
            // 将起始时间与结束时间的String格式转为deteTime格式
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");   //设置日期格式
            LocalDateTime begin = LocalDateTime.parse(beginTime,dateTimeFormatter);   // 格式转换
            LocalDateTime end = LocalDateTime.parse(endTime,dateTimeFormatter);       // 格式转换
            queryWrapper.between(Orders::getOrderTime,begin,end);   // 范围查询
        }
        // 执行分页查询
        ordersService.page(ordersPage,queryWrapper);

        return R.success(ordersPage);
    }

    /**
     * 操作:派送
     * @return
     */
    @PutMapping
    public R<Orders> putOrders(@RequestBody Orders orders){
        log.info("派送操作:{}",orders);
        // 根据id查询订单
        Orders ordersById = ordersService.getById(orders.getId());
        // 修改订单状态
        ordersById.setStatus(orders.getStatus());
        // 修改数据库
        ordersService.updateById(ordersById);
        return R.success(ordersById);
    }

    /**
     * 再来一单
     * @return
     */
    @PostMapping("/again")
    public R<List<ShoppingCart>> putAgain(@RequestBody Orders orders){
        log.info("再来一单的信息:{}",orders);
        //获取订单id
        Long ordersId = orders.getId();
        //查询订单明细
        LambdaQueryWrapper<OrderDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrderDetail::getOrderId,ordersId);
        List<OrderDetail> orderDetailList = orderDetailService.list(queryWrapper);
        //将订单明细中的菜品加入购物车
        List<ShoppingCart> shoppingCartList = new ArrayList<>();
        for (OrderDetail orderDetail : orderDetailList) {
            ShoppingCart shoppingCart = new ShoppingCart();
            shoppingCart.setUserId(BaseContext.getCurrentId());     //设置用户id
            shoppingCart.setDishId(orderDetail.getDishId());        //设置菜品id
            shoppingCart.setSetmealId(orderDetail.getSetmealId());  //设置套餐id
            shoppingCart.setDishFlavor(orderDetail.getDishFlavor());//设置口味
            shoppingCart.setNumber(orderDetail.getNumber());        //设置数量
            shoppingCart.setAmount(orderDetail.getAmount());        //设置金额
            shoppingCart.setImage(orderDetail.getImage());          //设置图片
            shoppingCartList.add(shoppingCart);
        }
        shoppingCartService.saveBatch(shoppingCartList);

        return R.success(shoppingCartList);
    }
}
